var GlobalErrorListener = require('./GlobalErrorListener');
var a = function (e) {
    for (var t = e.str; e.cur < t.length;) {
        var i = t[e.cur];
        if (' ' !== i && '\n' !== i && '\r' !== i && '\t' !== i && '\f' !== i)
            break;
        e.cur += 1;
    }
    return e.cur < t.length;
};
var r = function (e) {
    var t = e.str;
    for (var i = ''; e.cur < t.length;) {
        var n = t[e.cur];
        if (!(n >= '0' && n <= '9'))
            break;
        i += n;
        e.cur += 1;
    }
    if (!i)
        throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (illegal index)'));
    return parseInt(i, 10);
};
var o = function (e) {
    var t = e.str;
    for (var i = ''; e.cur < t.length;) {
        var n = t[e.cur];
        if ('\\' === n)
            if (e.cur + 1 === t.length);
            else
                i += t[e.cur + 1], e.cur += 1;
        else if (/^[_a-zA-Z]$/.test(n))
            i += n;
        else {
            if (!(n >= '0' && n <= '9'))
                break;
            if ('' === i)
                throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (field name cannot start with digits)'));
            i += n;
        }
        e.cur += 1;
    }
    return i;
};
var s = function (e) {
    var t = e.str;
    for (var i = ''; e.cur < t.length;) {
        var n = t[e.cur];
        if ('\\' === n)
            if (e.cur + 1 === t.length)
                i += '\\', e.cur += 1;
            else {
                var a = t[e.cur + 1];
                if ('.' === a || '[' === a || ']' === a || '\\' === a) {
                    i += a;
                    e.cur += 2;
                } else {
                    i += '\\';
                    e.cur += 1;
                }
            }
        else {
            if ('.' === n || '[' === n)
                break;
            i += n;
            e.cur += 1;
        }
    }
    return i;
};
var l = function (e) {
    var t = e.str;
    a(e);
    if ('*' === t[e.cur] && '*' === t[e.cur + 1]) {
        e.cur += 2;
        return ['**'];
    }
    var i = o(e);
    if (!i)
        throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (first field name illegal)'));
    for (var n = [i]; e.cur < t.length && a(e);) {
        var s = t[e.cur];
        if ('.' === s) {
            e.cur += 1;
            a(e);
            if ('*' === t[e.cur] && '*' === t[e.cur + 1]) {
                n.push('**');
                e.cur += 2;
                break;
            }
            var l = o(e);
            if (!l)
                throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (field name illegal)'));
            n.push(l);
        } else {
            if ('[' !== s)
                break;
            e.cur += 1;
            a(e);
            var d = r(e);
            a(e);
            if (']' !== t[e.cur])
                throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (illegal index)'));
            e.cur += 1;
            n.push(d);
        }
    }
    return n;
};
exports.parseSinglePath = function (e) {
    var t = function (e) {
        var t = e.str;
        for (var i = []; e.cur < t.length;) {
            var n = t[e.cur];
            if ('.' === n) {
                e.cur += 1;
                var a = s(e);
                a && i.push(a);
            } else if ('[' === n) {
                e.cur += 1;
                var o = r(e);
                if (']' !== t[e.cur] && e.cur < t.length)
                    throw new Error('data path descriptor "'.concat(t, '" is illegal at char ').concat(e.cur, ' (illegal index)'));
                e.cur += 1;
                i.push(o);
            } else {
                var l = s(e);
                l && i.push(l);
            }
        }
        return i;
    }({
        str: e,
        cur: 0
    });
    return t;
};
exports.parseMultiPaths = function (e) {
    try {
        var t = {
            str: e,
            cur: 0
        };
        var i = function (e) {
            var t = [];
            t.push(l(e));
            for (a(e); ',' === e.str[e.cur];)
                e.cur += 1, t.push(l(e)), a(e);
            return t;
        }(t);
        if (t.cur !== e.length)
            throw new Error('data path descriptor "'.concat(e, '" is illegal at char ').concat(t.cur));
        return i;
    } catch (e) {
        GlobalErrorListener.triggerWarning(e.message);
        return [];
    }
};
